package com.ruoyi.hl.task;

import com.alibaba.fastjson.JSONObject;
import com.google.gson.Gson;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.hl.reqbean.AddBanZuZhangBean;
import com.ruoyi.hl.reqbean.ShouHuanAddChuanYun;
import com.ruoyi.hl.resultbean.SheBeiListBean;
import com.ruoyi.hl.resultbean.TokenBean;
import com.ruoyi.hl.service.ShouHuanService;
import com.ruoyi.utils.HttpClient;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.*;

@Component("shouhuantask")
public class ZhongDianRenYuanTask {
    @Autowired
    private ShouHuanService shouHuanService;

    public void ryNoParams() {
        getBloodList();
        getHeartList();
        getTemperatureList();
    }

    //兔盯云token
    public String getToken() {
        String result = "";
        String url = "http://47.92.252.3:18001/tsp/auth/token";
        String appkey = "10016";
        Date d = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
        String dateNowStr = sdf.format(d);
        String transid = appkey + dateNowStr + "WkCRvFdYxyLy";
        String secret = "5c06516775d469481f156b7b0708dacb";
        String sign = DigestUtils.sha1Hex(secret + appkey + transid + secret);
        Map<String, String> map = new HashMap<>();
        map.put("transid", transid);
        map.put("appkey", appkey);
        map.put("sign", sign);
        result = HttpClient.doGet(url, map, null);
        TokenBean tokenBean = JSONObject.parseObject(result, TokenBean.class); //json字符串直接转给java对象
        return tokenBean.getData().getToken();
    }

    //查询所有设备数据
    public List<SheBeiListBean.DataBean.ResultBean> getAllList() {
        List<SheBeiListBean.DataBean.ResultBean> list = new ArrayList<>();
        String token = getToken();
        String result = "";
        String result2 = "";
        String url = "http://47.92.252.3:18001/tsp/devices/get";
        String appkey = "10016";
        Date d = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
        String dateNowStr = sdf.format(d);
        String transid1 = appkey + dateNowStr + "WkCRvFdYxyLb";
        String transid2 = appkey + dateNowStr + "WkCRvFdYxyLc";
        Map<String, String> map = new HashMap<>();
        map.put("page", "1");
        map.put("limit", "100");
        Map<String, String> headers = new HashMap<>();
        headers.put("transid", transid1);
        headers.put("token", token);
        result = HttpClient.doGet(url, map, headers);
        SheBeiListBean sheBeiListBean = JSONObject.parseObject(result, SheBeiListBean.class); //json字符串直接转给java对象
        if (sheBeiListBean != null && sheBeiListBean.getData() != null && sheBeiListBean.getData().getResult() != null) {
            list = sheBeiListBean.getData().getResult();
        }
        if (list.size() == 100) {
            List<SheBeiListBean.DataBean.ResultBean> list2 = new ArrayList<>();
            Map<String, String> map2 = new HashMap<>();
            map2.put("page", "2");
            map2.put("limit", "100");
            Map<String, String> headers2 = new HashMap<>();
            headers2.put("transid", transid2);
            headers2.put("token", token);
            result2 = HttpClient.doGet(url, map2, headers2);
            SheBeiListBean sheBeiListBean2 = JSONObject.parseObject(result2, SheBeiListBean.class); //json字符串直接转给java对象
            if (sheBeiListBean2 != null && sheBeiListBean2.getData() != null && sheBeiListBean2.getData().getResult() != null) {
                list2 = sheBeiListBean2.getData().getResult();
            }
            list.addAll(list2);
        }
        return list;
    }

    //获取所有班组长数据
    public List<AddBanZuZhangBean> selectBanZhuList(AddBanZuZhangBean addBanZuZhangBean) {
        return shouHuanService.selectBanZuInfo(addBanZuZhangBean);
    }

    /**
     * 获取山金云数据库每台设备最新一条血压数据列表
     */
    List<SheBeiListBean.DataBean.ResultBean> shebeiList = new ArrayList<>();
    //获取所有班组长数据
    List<AddBanZuZhangBean> banzhuList = new ArrayList<>();

    public void getBloodList() {
        shebeiList = getAllList();
        banzhuList = selectBanZhuList(new AddBanZuZhangBean());
        List<ShouHuanAddChuanYun> newList = shouHuanService.selectBloodDetail();
        if (null != getAllList() && null != newList) {
            for (ShouHuanAddChuanYun item : newList) {
                for (SheBeiListBean.DataBean.ResultBean item2 : shebeiList) {
                    if (item.getImei_sn().equals(item2.getImei_sn())) {
                        item.setUserName(item2.getTruename());
                        if (!StringUtils.isEmpty(item.getRate()) && item.getRate().contains("-")) {
                            String[] arr = item.getRate().split("-");
                            item.setLow_blood(Integer.parseInt(arr[0]));
                            item.setHigh_blood(Integer.parseInt(arr[1]));
                        }
                    }
                }
                if (null != banzhuList && banzhuList.size() > 0) {
                    for (AddBanZuZhangBean item2 : banzhuList) {
                        if (item.getImei_sn().equals(item2.getEquipmentId()) && !StringUtils.isEmpty(item2.getUserId())) {
                            item.setBzzId(item2.getUserId().split(","));
                        }
                    }
                }
            }
            createCyTable(newList, "D10541250daa7555a3049459ef408079daaf739");
        }
    }

    /**
     * 获取山金云数据库每台设备最新一条血压数据列表
     */
    public void getHeartList() {
        List<ShouHuanAddChuanYun> newList = shouHuanService.selectHeartDetail();
        if (null != getAllList() && null != newList) {
            for (ShouHuanAddChuanYun item : newList) {
                for (SheBeiListBean.DataBean.ResultBean item2 : shebeiList) {
                    if (item.getImei_sn().equals(item2.getImei_sn())) {
                        item.setUserName(item2.getTruename());
                    }
                }
                if (null != banzhuList && banzhuList.size() > 0) {
                    for (AddBanZuZhangBean item2 : banzhuList) {
                        if (item.getImei_sn().equals(item2.getEquipmentId()) && !StringUtils.isEmpty(item2.getUserId())) {
                            item.setBzzId(item2.getUserId().split(","));
                        }
                    }
                }
            }
            createXlTable(newList, "D10541223a1cf2d7585452bb82d28aa9dfd4a04");
        }
    }

    /**
     * 获取山金云数据库每台设备最新一条血压数据列表
     */
    public void getTemperatureList() {
        List<ShouHuanAddChuanYun> newList = shouHuanService.selectTemperatureDetail();
        if (null != getAllList() && null != newList) {
            for (ShouHuanAddChuanYun item : newList) {
                for (SheBeiListBean.DataBean.ResultBean item2 : shebeiList) {
                    if (item.getImei_sn().equals(item2.getImei_sn())) {
                        item.setUserName(item2.getTruename());
                    }
                }
                if (null != banzhuList && banzhuList.size() > 0) {
                    for (AddBanZuZhangBean item2 : banzhuList) {
                        if (item.getImei_sn().equals(item2.getEquipmentId()) && !StringUtils.isEmpty(item2.getUserId())) {
                            item.setBzzId(item2.getUserId().split(","));
                        }
                    }
                }
            }
            createTwTable(newList, "D105412f4c71f5f518a4881b50d1051af3f19bc");
        }
    }

    //数据插入氚云--血压
    public void createCyTable(List<ShouHuanAddChuanYun> list, String schemaCode) {
        String url = "https://www.h3yun.com/OpenApi/Invoke";
        Gson gson = new Gson();
        Map<String, Object> map = new HashMap<>();
        map.put("ActionName", "CreateBizObjects");
        map.put("SchemaCode", schemaCode);
        map.put("IsSubmit", "true");
        List<String> chilList = new ArrayList<>();
        if (null != list && list.size() > 0) {
            for (ShouHuanAddChuanYun item : list) {
                Map<String, Object> childtableMap = new HashMap<>();
                childtableMap.put("CreatedBy", "3225cfee-dca2-439b-9cec-14e1a5d763e3");
                childtableMap.put("OwnerId", "3225cfee-dca2-439b-9cec-14e1a5d763e3");
                childtableMap.put("F0000001", item.getUserName());//姓名
                childtableMap.put("F0000002", item.getImei_sn());
                childtableMap.put("F0000003", item.getHigh_blood());
                childtableMap.put("F0000004", item.getLow_blood());
                childtableMap.put("F0000005", item.getUpdateTime());
                childtableMap.put("F0000006", item.getBzzId());
                chilList.add(gson.toJson(childtableMap));
            }
        }
        map.put("BizObjectArray", chilList);
        try {
            if (chilList.size() > 0) {
                HttpClient.sendPostDataByJson(url, gson.toJson(map));
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    //数据插入氚云--心率
    public void createXlTable(List<ShouHuanAddChuanYun> list, String schemaCode) {
        String url = "https://www.h3yun.com/OpenApi/Invoke";
        Gson gson = new Gson();
        Map<String, Object> map = new HashMap<>();
        map.put("ActionName", "CreateBizObjects");
        map.put("SchemaCode", schemaCode);
        map.put("IsSubmit", "true");
        List<String> chilList = new ArrayList<>();
        if (null != list && list.size() > 0) {
            for (ShouHuanAddChuanYun item : list) {
                Map<String, Object> childtableMap = new HashMap<>();
                childtableMap.put("CreatedBy", "3225cfee-dca2-439b-9cec-14e1a5d763e3");
                childtableMap.put("OwnerId", "3225cfee-dca2-439b-9cec-14e1a5d763e3");
                childtableMap.put("F0000001", item.getUserName());//姓名
                childtableMap.put("F0000002", item.getImei_sn());
                childtableMap.put("F0000003", Integer.parseInt(item.getRate()));
                childtableMap.put("F0000013", item.getUpdateTime());
                childtableMap.put("F0000014", item.getBzzId());
                chilList.add(gson.toJson(childtableMap));
            }
        }
        map.put("BizObjectArray", chilList);
        try {
            if (chilList.size() > 0) {
                HttpClient.sendPostDataByJson(url, gson.toJson(map));
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    //数据插入氚云--体温
    public void createTwTable(List<ShouHuanAddChuanYun> list, String schemaCode) {
        String url = "https://www.h3yun.com/OpenApi/Invoke";
        Gson gson = new Gson();
        Map<String, Object> map = new HashMap<>();
        map.put("ActionName", "CreateBizObjects");
        map.put("SchemaCode", schemaCode);
        map.put("IsSubmit", "true");
        List<String> chilList = new ArrayList<>();
        if (null != list && list.size() > 0) {
            for (ShouHuanAddChuanYun item : list) {
                Map<String, Object> childtableMap = new HashMap<>();
                childtableMap.put("CreatedBy", "3225cfee-dca2-439b-9cec-14e1a5d763e3");
                childtableMap.put("OwnerId", "3225cfee-dca2-439b-9cec-14e1a5d763e3");
                childtableMap.put("F0000001", item.getUserName());//姓名
                childtableMap.put("F0000002", item.getImei_sn());
                childtableMap.put("F0000003", Double.parseDouble(item.getRate()));
                childtableMap.put("F0000004", item.getUpdateTime());
                childtableMap.put("F0000005", item.getBzzId());
                chilList.add(gson.toJson(childtableMap));
            }
        }
        map.put("BizObjectArray", chilList);
        try {
            if (chilList.size() > 0) {
                HttpClient.sendPostDataByJson(url, gson.toJson(map));
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}
